var inited=false,
    mapInited=false;
//初始化时间
var today=new Date();
var curTime=new Date(today);
today.setHours(0,0,0,0);
//初始化模块加载项为 首页
var curModule=0;
//初始化数据
var REAL_MODE_DATA={
};

$(document).ready(function(){
    console.log("loading overView");
    initOptions();
    resetData();
    initData();
    timer=setInterval(update,1000);
});


/**
 * 更新异常行为数据
 * @param {number} startTime 
 * @param {number} endTime 
 */
function updateAbnormalEventsData(startTime,endTime){
    $.get("./assets/mockdata/abnormalEvents.json",{
        startTime:startTime,
        endTime:endTime,
        type:"allAbnormal",
        subType:"",
        limitSize:-1,
        isDesc:1
    },function(data,status){
        if(status=="success"){
                REAL_MODE_DATA.abnormalData.startTime=data.startTime;
                REAL_MODE_DATA.abnormalData.endTime=data.endTime;
                REAL_MODE_DATA.abnormalData.data=data.data;   
        }

        if("undefined" == typeof loadAbnormalData)
        {
            
        }
        else{
            console.log("应用异常行为数据中");
            loadAbnormalData();
        }
    });
}

/**
 * 更新攻击事件数据
 * @param {number} startTime 
 * @param {number} endTime 
 */
function updateAttackEventsData(startTime,endTime){
    $.get("./assets/mockdata/attackEvents.json",{
        startTime:startTime,
        endTime:endTime,
        type:"attack",
        subType:"",
        limitSize:-1,
        isDesc:1
    },function(data,status){
        if(status=="success"){
                REAL_MODE_DATA.attackData.startTime=data.startTime;
                REAL_MODE_DATA.attackData.endTime=data.endTime;
                REAL_MODE_DATA.attackData.data=data.data;                
        }
        if("undefined" == typeof loadAttackData)
        {
            
        }
        else{
            console.log("应用攻击事件数据中");
            loadAttackData();
        }
    });
}

/**
 * 更新web流量数据
 * @param {number} startTime 
 * @param {number} endTime 
 */
function updateWebStreamData(startTime,endTime){
    $.get("./assets/mockdata/webStream.json",{
        startTime:startTime,
        endTime:endTime,
        type:"web",
    },function(data,status){
        if(status=="success"){
                REAL_MODE_DATA.webStreamData.startTime=data.startTime;
                REAL_MODE_DATA.webStreamData.endTime=data.endTime;
                REAL_MODE_DATA.webStreamData.data=data.data;  
                REAL_MODE_DATA.webStreamData.srcData=data.srcData;
                REAL_MODE_DATA.webStreamData.feature=data.feature; 
        }
        if("undefined" == typeof loadWebStreamData)
        {

        }
        else{
            console.log("应用Web流量数据中");
            loadWebStreamData();
        }      
    });
}

/**
 * 更新防火墙流量数据
 * @param {number} startTime 
 * @param {number} endTime 
 */
function updateFirewallStreamData(startTime,endTime){
    $.get("./assets/mockdata/firewallStream.json",{
        startTime:startTime,
        endTime:endTime,
        type:"firewall",
    },function(data,status){
        if(status=="success"){
                REAL_MODE_DATA.firewallStreamData.startTime=data.startTime;
                REAL_MODE_DATA.firewallStreamData.endTime=data.endTime; 
                REAL_MODE_DATA.firewallStreamData.data=data.data;
                REAL_MODE_DATA.firewallStreamData.srcData=data.srcData;
                REAL_MODE_DATA.firewallStreamData.feature=data.feature;                       
        }
        if("undefined" == typeof loadFirewallStreamData)
        {   

        }
        else{
            console.log("应用防火墙流量数据中");
            loadFirewallStreamData();
        } 
    });
}

function updateTime(){
    //更新当前时间
    curTime=new Date();
    //更新时间盘
    var chart = echarts.getInstanceByDom(document.getElementById('timeChart'));
    var num=(curTime.getHours()*60+curTime.getMinutes())/1440;
    var option={
        series: [{
            axisLine: { // 仪表盘轴线
                lineStyle: {
                    //24*4=96格
                    color: [
                        [num, '#fff'],
                        [1, '#34364d']
                    ],

                }
            },
        }]
    };
    chart.setOption(option);
}

/**
 * 更新逻辑
 */
function update(){
    updateTime();
    console.log("updating...");
    $("#menu-container h2").text(`${curTime.toLocaleString()}`);
    //检查是否为新的一天
    if(curTime.getDate()!=today.getDate()){
        //更新今日时间
        today=curTime;
        today=new Date(curTime).setHours(0,0,0,0);
        //初始化实时数据
        resetData();
        initData();
    }
    //如果仍是今天
    else{
        //根据设定实时更新数据
        if(curTime.getSeconds()==0){
            //查询并更新实时数据
            updateWebStreamData(+curTime-6000,+curTime,true);
            updateFirewallStreamData(+curTime-15*6000,+curTime,true);
        }
        //整刻时更新攻击态势和异常态势数据。
        if(((curTime.getMinutes())%15==0)&&curTime.getSeconds()==0){
            updateAbnormalEventsData(+today,+curTime,true);
            updateAttackEventsData(+today,+curTime,true);
        }
    }

}

/**
 * 重设数据。用于初始化取得数据前以及新一天开始后的数据初始。
 */
function resetData(){
    REAL_MODE_DATA={
        webStreamData:{
            startTime:"2018-05-01 15:00:00",
            endTime:"2018-05-01 15:01:00",
            data:[
                {
                    startTime:"2018-05-01 15:00:00",
                    endTime:"2018-05-01 15:01:00",
                    curStream:66,
                    maxStream:75,
                    minStream:50
                }
            ],
            srcData:{
                '杭州':852,
                '南京':445,
                '北京':298,
                '宁波':395,
                '乌鲁木齐':195,
                '嘉兴':175
            },
            feature:{
                '200':852,
                '302':450,
                '304':320,
                '305':89
            }
        },
        firewallStreamData:{
            startTime:"2018-05-01 15:00:00",
            endTime:"2018-05-01 15:15:00",
            data:[
                {
                    startTime:"2018-05-01 15:00:00",
                    endTime:"2018-05-01 15:01:00",
                    curStream:66,
                    maxStream:75,
                    minStream:50
                },
                {
                    startTime:"2018-05-01 15:01:00",
                    endTime:"2018-05-01 15:02:00",
                    curStream:60,
                    maxStream:77,
                    minStream:51
                },
                {
                    startTime:"2018-05-01 15:02:00",
                    endTime:"2018-05-01 15:03:00",
                    curStream:71,
                    maxStream:88,
                    minStream:63
                }
            ],
            srcData:{
                '杭州':852,
                '南京':445,
                '北京':298,
                '宁波':395,
                '乌鲁木齐':195,
                '嘉兴':175
            },
            feature:{
                '200':852,
                '302':450,
                '304':320,
                '305':89
            }
        },
        abnormalData:{
            startTime:"2018-05-01 00:00:00",
            endTime:"2018-05-02 00:00:00",
            data:[
                {
                    startTime:"2018-05-01 04:02:02",
                    endTime:"2018-05-01 05:00:05",
                    eventType:"ipAbnormal",
                    feature:{
                        dstIp:"172.17.64.38",
                        value:888
                    }
                },
                {
                    startTime:"2018-05-01 05:02:02",
                    endTime:"2018-05-01 06:00:05",
                    eventType:"portAbnormal",
                    feature:{
                        dstPort:"8080",
                        dstIp:"172.17.64.38",
                        value:777
                    }
                },
                {
                    startTime:"2018-05-01 06:02:02",
                    endTime:"2018-05-01 07:00:05",
                    eventType:"streamAbnormal",
                    feature:{
                        type:"web",
                        value:999999
                    }
                },{
                    startTime:"2018-05-01 07:02:02",
                    endTime:"2018-05-01 08:00:05",
                    eventType:"streamAbnormal",
                    feature:{
                        type:"firewall",
                        value:8888
                    }
                },{
                    startTime:"2018-05-01 05:02:02",
                    endTime:"2018-05-01 06:00:05",
                    eventType:"visitorAbnormal",
                    feature:{
                        srcIp:"172.13.1.16",
                        srcCity:"杭州",
                        value:999
                    }
                }
            ]
        },
        attackData:{
            startTime:"2018-05-01 00:00:00",
            endTime:"2018-05-02 00:00:00",
            data:[
                {
                    startTime:"2018-05-01 05:02:02",
                    endTime:"2018-05-01 06:00:05",
                    eventType:"attack",
                    feature:{
                        type:"跨站脚本攻击",
                        srcIp:"113.15.16.21",
                        srcCity:"杭州"
                    }
                }
            ]
        }
    };
 
}


/**
 * 初始化数据。用于第一次运行时初始化。
 */
function initData(){
    var end=new Date(curTime).setSeconds(0,0);
    updateWebStreamData(+end-60000,+end,true);
    updateFirewallStreamData(+end-15*6000,+end,true);
    updateAbnormalEventsData(+today,+curTime,true);
    updateAttackEventsData(+today,+curTime,true);
    //加载完整流量数据
    updateWebStreamData(+today,+end,true);
    updateFirewallStreamData(+today,+end,true);
}